/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#ifndef IMAGEPNG_H
#define IMAGEPNG_H


#include "IncFreeTrack.h"
#include "image.h"

#define PNG_NO_SETJMP
#include "libpng/png.h"

class ImagePng : public Image
{
public:
    // Used to load a jpeg
    ImagePng( const QString& );
    // We need to free the libjpeg data structures
    ~ImagePng();
    // Check if this is a valid jpeg (use only on existing files)
    bool isValid();

    // Read cycle: call this first...
    bool startReading( int zoom, int& output_width, int& output_height );
    // ...then this until all image has been read!
    uchar* readScanline();

    // Quick function to read an image into a QImage
    bool readImage( QImage* image,
                    const QRect& source_rect = QRect(),
                    const QRect& dest_rect = QRect(),
                    int zoom = 1 );

private:
    // I prefer to use simply an old-style FILE* for libpng...
    FILE* pFile;
    int bit_depth;
    int color_type;
    int interlace_type;
    int channels;
    int row_bytes;
    int full_width;
    int full_height;
    png_bytep row_pointer;
    int current_row;

private: // read data section
    // The libpng read structure
    png_structp png_ptr;
    // The libpng info structure
    png_infop info_ptr;
    // As opposed to libjpg, libpng does not support scaling internally, so we will implement all
    // zoom levels here in the code (1,2,4,8,16,32,64,128,256).
    int scale_factor;

};

#endif // IMAGEPNG_H
